import ping
from socket import gethostbyname
from operator import itemgetter
result = open("result.txt","w")
hosts = open("hosts.txt","w")
preiplist=[]
for a in range(99):
    host = str("a%d.phobos.apple.com")%(a+1)
    ip = gethostbyname(host)
    print("Ping %s")%(host)
    try:
        totaltime=0
        time=0
        for c  in range(10):
            totaltime += ping.do_one(ip,1,64)
        time = totaltime/10
    except :
        time=0
    if (time):
        preiplist.append([ip,time])
iplist=sorted(preiplist,key=itemgetter(1))
for i in iplist:
    result.write("%s\t%.4f\r\n"%(i[0],i[1]))
result.close
bestip=iplist[0][0]
for j in range(9):
    hosts.write("%s\ta%d.phobos.apple.com\r\n"%(bestip,j+1))
    hosts.write("%s\ta0%d.phobos.apple.com\r\n"%(bestip,j+1))
for k in range(10,100):
    hosts.write("%s\ta%d.phobos.apple.com\r\n"%(bestip,k))
hosts.close



